﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Maticsoft.DAL;
using System.Reflection;
using System.IO;
using Commons.CommonHelper;

namespace BuildingForm
{
    public partial class StayInfoManage : Form, IModuleExtension 
    {
        public StayInfoManage()
        {
            InitializeComponent();
            Init();
            bind();
        }
        private int nid = 0;
        private MyOpaqueLayer.MyOpaqueLayer m_OpaqueLayer = null;
        public void Init()
        {
            this.cboState.SelectedIndexChanged -= new System.EventHandler(this.cboState_SelectedIndexChanged);
            this.cboState2.SelectedIndexChanged -= new System.EventHandler(this.cboState2_SelectedIndexChanged);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowIcon = false;
            //表头加粗，居中
            foreach (DataGridViewColumn col in this.dataGridView1.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.dataGridView1.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.txtName.Text = "";
            
            //班组
            Maticsoft.DAL.Banz_Info info = new Banz_Info();
            DataTable dt = info.GetList("").Tables[0];
            DataTable dt2 = info.GetList("").Tables[0];
            DataRow dr3 = dt.NewRow();
            dr3["bzid"] = 0;
            dr3["bzmc"] = "全部";
            dt.Rows.InsertAt(dr3, 0);
            this.cboTeam.DisplayMember = "bzmc";
            this.cboTeam.ValueMember = "bzid";
            this.cboTeam.DataSource = dt;
            this.cboTeam2.DisplayMember = "bzmc";
            this.cboTeam2.ValueMember = "bzid";
            this.cboTeam2.DataSource = dt2;
            //状态
            Dictionary<int,string> dic=new Dictionary<int,string> ();
            dic.Add(0, "全部");
            dic.Add(1, "在住");
            dic.Add(2, "外住");
            BindingSource bs = new BindingSource();
            bs.DataSource = dic;
            this.cboState.DisplayMember = "Value";
            this.cboState.ValueMember = "Key";
            this.cboState.DataSource = bs;
            this.cboState.SelectedIndex = 0;

            Dictionary<int, string> dic2= new Dictionary<int, string>();
            dic2.Add(1, "在住");
            dic2.Add(2, "外住");
            BindingSource bs2 = new BindingSource();
            bs2.DataSource = dic2;
            this.cboState2.DisplayMember = "Value";
            this.cboState2.ValueMember = "Key";
            this.cboState2.DataSource = bs2;
            this.cboState2.SelectedIndex = 0;
            //位置
            Maticsoft.DAL.Building buildingDao = new Maticsoft.DAL.Building();
            Maticsoft.DAL.Room roomDao = new Maticsoft.DAL.Room();
            DataTable dtBuilding = buildingDao.GetList("").Tables[0];
            DataTable dtRoom = roomDao.GetList("").Tables[0];
            DataTable dtBuildingCopy = dtBuilding.Copy();
            DataTable dtRoomCopy = dtRoom.Copy();
            DataRow dr1 = dtBuildingCopy.NewRow();
            dr1["BuildingName"] = "";
            dtBuildingCopy.Rows.InsertAt(dr1, 0);
            DataRow dr2 = dtRoomCopy.NewRow();
            dr2["RoomName"] = "";
            dtRoomCopy.Rows.InsertAt(dr2, 0);
            this.cboA.DisplayMember = "BuildingName";
            this.cboA.ValueMember = "BuildingName";
            this.cboA.DataSource = dtBuildingCopy;
            this.cboB.DisplayMember = "RoomName";
            this.cboB.ValueMember = "RoomName";
            this.cboB.DataSource = dtRoomCopy;
            this.cboAA.DisplayMember = "BuildingName";
            this.cboAA.ValueMember = "BuildingName";
            this.cboAA.DataSource = dtBuilding;
            this.cboBB.DisplayMember = "RoomName";
            this.cboBB.ValueMember = "RoomName";
            this.cboBB.DataSource = dtRoom;

            this.panel1.BorderStyle = BorderStyle.FixedSingle;
            panel1.Location = new Point((this.Size.Width - panel1.Size.Width) / 2, (this.Size.Height - panel1.Size.Height) / 2);
            dataGridView1.AllowUserToAddRows = false;
            //位置
            this.cboA.Enabled = false;
            this.cboB.Enabled = false;
            this.cboState.SelectedIndexChanged += new System.EventHandler(this.cboState_SelectedIndexChanged);
            this.cboState2.SelectedIndexChanged += new System.EventHandler(this.cboState2_SelectedIndexChanged);
            hide();
        }
        public DataTable bind()
        {
            string name = txtName.Text.ToString().Trim();
            string team = cboTeam.SelectedValue.ToString();
            string state = cboState.SelectedValue.ToString();
            string wa = cboA.Text.ToString();
            string wb = cboB.Text.ToString();
            string sql = "1=1";
            if (!string.IsNullOrEmpty(name))
            {
                sql += " and a.xm like '%" + name + "%'";
            }
            if (team!="0")
            {
                sql += " and c.bzid='" + team + "'";
            }
            if (state!="0")
            {
                sql += " and d.state=" + state;
            }
            if (state == "1")
            {
                if (!string.IsNullOrEmpty(wa))
                {
                    sql += " and d.wza='" + wa + "'";
                }
                if (!string.IsNullOrEmpty(wb))
                {
                    sql += " and d.wzb='" + wb + "'";
                }
            }
            sql += " order by d.CreateTime desc";
            Maticsoft.DAL.Ry_Stay_Info info = new Ry_Stay_Info();
            DataTable dt = info.GetListByStayInfo (sql).Tables[0];
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = dt;
            return dt;
        }
        private void show()
        {
            if (this.m_OpaqueLayer == null)
            {
                this.m_OpaqueLayer = new MyOpaqueLayer.MyOpaqueLayer(125, false);
                this.Controls.Add(this.m_OpaqueLayer);
                this.m_OpaqueLayer.Dock = DockStyle.Fill;
                this.m_OpaqueLayer.BringToFront();
            }
            this.m_OpaqueLayer.Enabled = true;
            this.m_OpaqueLayer.Visible = true;

            //panel1在悬浮层上显示

            panel1.BackColor = Color.White;

            m_OpaqueLayer.Controls.Add(panel1);
            panel1.Show();
        }
        private void hide()
        {
            panel1.Hide();
            if (this.m_OpaqueLayer != null)
            {
                this.m_OpaqueLayer.Visible = false;
                this.m_OpaqueLayer.Enabled = false;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            bind();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "btnUpdate")
            {
                DataGridViewRow r = dataGridView1.Rows[e.RowIndex];
                int id = Convert.ToInt32(r.Cells[8].Value);
                nid = id;
                string xm = r.Cells[1].Value.ToString();
                string team = r.Cells[2].Value.ToString();
                string sfzh = r.Cells[3].Value.ToString();
                string bzid=r.Cells[9].Value.ToString ();
                lalName.Text = xm;
                lblSFZH.Text = sfzh;
                this.cboTeam2.SelectedIndex = GetBzidIndex(Convert.ToInt32(bzid));
                this.cboTeam2.Enabled = false;
                Maticsoft.DAL.Ry_Stay_Info rys = new Ry_Stay_Info();
                Maticsoft.Model.Ry_Stay_Info info = rys.GetModel(id);
                this.cboState2.SelectedIndex = (info.state - 1);
                if (info.state == 1)
                {
                    this.cboAA.Enabled = true;
                    this.cboBB.Enabled = true;
                    this.cboAA.SelectedIndex = GetIndexByBuilding(info.wza);
                    this.cboBB.SelectedIndex = GetIndexByRoom(info.wzb);
                }
                else
                {
                    this.cboAA.Enabled = false;
                    this.cboBB.Enabled = false;
                    this.cboAA.SelectedIndex = 0;
                    this.cboBB.SelectedIndex = 0;
                }
                show();
            }
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "btnDel")
            {
                if (MessageBox.Show("您确定要删除吗？", "提示：", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
                {
                    DataGridViewRow r = dataGridView1.Rows[e.RowIndex];
                    int id = Convert.ToInt32(r.Cells[8].Value);
                    Maticsoft.DAL.Ry_Stay_Info info = new Ry_Stay_Info();
                    bool isT=info.Delete(id);
                    if (isT)
                    {
                        MessageBox.Show("删除成功！");
                    }
                    else
                    {
                        MessageBox.Show("删除失败！");
                    }
                }
            }
            bind();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            hide();
        }

     

        private void cboState2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cboState.SelectedIndexChanged -= new System.EventHandler(this.cboState_SelectedIndexChanged);
            this.cboState2.SelectedIndexChanged -= new System.EventHandler(this.cboState2_SelectedIndexChanged);
            Maticsoft.DAL.Ry_Stay_Info rys = new Ry_Stay_Info();
            Maticsoft.Model.Ry_Stay_Info info = rys.GetModel(nid);
            if (info == null) return;
            int state = Convert.ToInt32(this.cboState2.SelectedValue);
            if (state == 1)
            {
                this.cboAA.Enabled = true;
                this.cboBB.Enabled = true;
                this.cboAA.SelectedIndex = GetIndexByBuilding(info.wza);
                this.cboBB.SelectedIndex = GetIndexByRoom(info.wzb);
            }
            else
            {
                this.cboAA.Enabled = false;
                this.cboBB.Enabled = false;
                this.cboAA.SelectedIndex = 0;
                this.cboBB.SelectedIndex = 0;
            }
            this.cboState.SelectedIndexChanged += new System.EventHandler(this.cboState_SelectedIndexChanged);
            this.cboState2.SelectedIndexChanged += new System.EventHandler(this.cboState2_SelectedIndexChanged);
        }

        private void btnUpdate_p1_Click(object sender, EventArgs e)
        {
            Maticsoft.DAL.Ry_Stay_Info rys = new Ry_Stay_Info();
            Maticsoft.Model.Ry_Stay_Info info = rys.GetModel(nid);
            string state = this.cboState2.SelectedValue.ToString();
            info.state = Convert.ToInt32(state);
            if (state == "1")
            {
                string wza = this.cboAA.Text.ToString();
                string wzb = this.cboBB.Text.ToString();
                info.wza = wza;
                info.wzb = wzb;
            }
            else
            {
                info.wza = "";
                info.wzb = "";
            }
            bool isT=rys.Update(info);
            if (isT)
            {
                MessageBox.Show("修改成功！"); 
            }
            else
            {
                MessageBox.Show("修改失败！");
            }
            hide();
            bind();
        }

        /// <summary>
        /// 调用窗口
        /// </summary>
        /// <param name="title">窗体名称</param>
        /// <param name="xmmc">项目名称</param>
        /// <returns></returns>
        public bool ShowExtension(string title, string xmmc)
        {
            try
            {
                this.ShowDialog();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载失败！");
                return false;
            }
        }

        private void cboState_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cboState.SelectedIndexChanged -= new System.EventHandler(this.cboState_SelectedIndexChanged);
            this.cboState2.SelectedIndexChanged -= new System.EventHandler(this.cboState2_SelectedIndexChanged);
            string state = this.cboState.SelectedValue.ToString();
            if (state == "1")
            {
                this.cboA.Enabled = true;
                this.cboB.Enabled = true;
            }
            else
            {
                this.cboA.Enabled = false;
                this.cboB.Enabled = false;
            }
            //this.cboState.SelectedIndex = Convert.ToInt32(state);
            this.cboState.SelectedIndexChanged += new System.EventHandler(this.cboState_SelectedIndexChanged);
            this.cboState2.SelectedIndexChanged += new System.EventHandler(this.cboState2_SelectedIndexChanged);
        }

        private void btnExistAdd_Click(object sender, EventArgs e)
        {
            BuildingForm.PersonInfo.AddStayPerson fr = new PersonInfo.AddStayPerson(this);
            fr.ShowDialog();
        }

        private int GetIndexByBuilding(string str)
        {
            Maticsoft.DAL.Building buildingDao = new Maticsoft.DAL.Building();
            DataTable dtBuilding = buildingDao.GetList("").Tables[0];      
            for (int i = 0; i < dtBuilding.Rows.Count; i++)
            {
                if (dtBuilding.Rows[i]["BuildingName"].ToString ().Trim() == str.Trim())
                {
                    return i;
                }
            }
            return 0;
        }
        private int GetIndexByRoom(string str)
        {
            Maticsoft.DAL.Room roomDao = new Maticsoft.DAL.Room();
            DataTable dtRoom = roomDao.GetList("").Tables[0];
            for (int i = 0; i < dtRoom.Rows.Count; i++)
            {
                if (dtRoom.Rows[i]["RoomName"].ToString().Trim() == str.Trim())
                {
                    return i;
                }
            }
            return 0;
        }
        private int GetBzidIndex(int bzid)
        {
            Maticsoft.DAL.Banz_Info info = new Banz_Info();
            DataTable dt = info.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int bzid2 =Convert .ToInt32( dt.Rows[i]["bzid"]);
                if (bzid2 == bzid)
                {
                    return (i + 1);
                }
            }
            return 0;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt= bind();
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("没有数据！"); return;
                }
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                //设置文件类型   
                saveFileDialog1.Filter = " xls files(*.xls)|*.xls";
                //设置默认文件类型显示顺序   
                saveFileDialog1.FilterIndex = 2;
                //保存对话框是否记忆上次打开的目录   
                saveFileDialog1.RestoreDirectory = true;
                saveFileDialog1.FileName = "人员住宿信息" + DateTime.Now.ToString("yyyyMMdd");
                //点了保存按钮进入   
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string team = this.cboTeam.Text.ToString().Trim();
                    string fileName = saveFileDialog1.FileName;
                    string state = cboState.SelectedValue.ToString();
                    string aa=cboA.SelectedValue .ToString ();
                    string bb=cboB.SelectedValue .ToString ();
                    string title=string.Empty;
                    if(state=="1"&&aa!=""&&bb!="")
                    {
                        title=aa+"#"+bb;
                    }
                    string[] oldCols = new string[] { "xh", "xm","gz","xb","age","mz","jg", "sfzh", "intime","outtime","fh" };
                    string[] newCols = new string[] { "序号", "姓名","工种","性别","年龄","民族","籍贯","身份证号","进场时间","退场时间","房号"};
                    ExcelHelper.ExportByStayInfo(dt, "人员住宿信息", fileName, "Sheet1", oldCols, newCols, title, dt.Rows.Count.ToString());
                    MessageBox.Show("导出文件成功！"); return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("导出文件故障！"); return;
            }
        }

        private void btnManageBuildingRoom_Click(object sender, EventArgs e)
        {
            BuildingForm.PersonInfo.ManageBuildingRoom mbr = new PersonInfo.ManageBuildingRoom(this);
            mbr.ShowDialog();
        }
    }
}
